<?php
class driver extends CI_Controller{
	private $data;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn()) redirect("/");
		$this->load->model("model_driver");
	}
		
	/*
			=================Function Add Driver=================
	*/
	public function index(){
		$data['addDriver'] = 'y';
		$data['masterinput'] = 'y';
		$data['merk'] = $this->model_driver->getMerk();
		$data['added'] = $this->model_driver->getNewlyAdd();
		$data['cat'] = $this->model_driver->getCat();
		$data['title'] = TITLE.' - Tambah Data Supir';
		$sFlag =  $this->uri->segment(3, 0);
		if($this->input->post("btnSave") ){
			
			$hasil = $this->_addNewDriver();
			if($hasil) 
			{
				$data['success'] = "Data Barang Berhasil Ditambahkan";
			
				redirect('/driver/index/s', 'refresh');
			}
		}
		if($sFlag==='d'){
					$data['success'] = "Sukses Dihapus";
				}
				else if($sFlag!=""){
					$data['success'] = "Input Sukses";
		}

		
		
		
		$this->load->view("driverAdd",$data);
		
	}
	
	
	
	function _addNewDriver(){
		//$this->form_validation->set_message('_checkpassword', 'Current Password is incorrect');
		$this->form_validation->set_message('_checkDriverAva', 
							'Barang Sudah ada');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'barcode',
                     'label'   => 'barcode',
                     'rules'   => 'trim|required|xss_clean|callback__checkDriverAva['
					 										.$this->input->post('barcode').']'
                  ),
               /*	array(
                     'field'   => 'gaji',
                     'label'   => 'Gaju/hari',
                     'rules'   => 'trim|required|xss_clean|integer'
                  ),*/
			 	
				
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_driver->addBarang();
			return true;
		}
	}
	
	function _checkDriverAva($barcode){
		$benar = 0;
		$data = $this->model_driver->getDataSupir($barcode);
		if($data){
			return true	;
		}else{
			return false;
		}
	}
	function addMerk(){
		$bookm = $_POST['bookm'];
		$this->model_driver->addMerk($bookm);
	}
	
	function ajaxLoadMerk(){
		$data['merk']=$this->model_driver->getMerk();
		$this->load->view("itemAddLoadMerk", $data);
	}
	
	function addCat(){
		$bookn = $_POST['bookn'];
		$this->model_driver->addCat($bookn);
	}
	
	function ajaxLoadCat(){
		$data['cat']=$this->model_driver->getCat();
		$this->load->view("itemAddLoadCat", $data);
	}
	
	/*
			=================Function View Driver=================
	*/
	public function view(){
		$data['masterview'] = 'y';
		$data['viewdriver'] = 'y';
		$sFlag =  $this->uri->segment(3, 0);
		$data['title'] = TITLE.' - Lihat Daftar Barang'; 
		$data['merk'] = $this->model_driver->getMerk();
		$data['cat'] = $this->model_driver->getCat();
		$data['added'] = $this->model_driver->getBarangList();
		if($sFlag==='d'){
			$data['success'] = "Sukses Dihapus";
		} 
		$this->load->view("driverview",$data);
	}
	function ajaxSearchDriver(){
		
		$dicari = $_POST['nilai'];
		$merk = $_POST['merk'];
		$cat = $_POST['cat'];
		$stok = $_POST['stok'];
		$mode = $_POST['mymode'];
		$data['added'] = $this->model_driver->ajaxGetSearchItem($stok,$dicari, $merk, $cat);
		
		$this->load->view("driverAjaxSearchRes", $data);
		
		
		
	}
	/*
			=================Function EditDriver=================
	*/
	function edit(){
		$drivid = $this->uri->segment(3, 0);		
		$sFlag =  $this->uri->segment(5, 0);
		$position = $this->uri->segment(4, 0);
		$data['masterview'] = 'y';
		$data['viewdriver'] = 'y';
		
		$data['barangDet'] = $this->model_driver->getBarangDet($drivid);
		$data['merk'] = $this->model_driver->getMerk();
		$data['added'] = $this->model_driver->getNewlyAdd();
		$data['cat'] = $this->model_driver->getCat();		
		$data['title'] = TITLE.' - Ubah Data Supir'; 
		$data['id'] = $drivid;
		
		if($this->input->post("btnSave")){
			$hasil = $this->_editDriver($data['id']);
			if($hasil) {
				$data['success'] = "Data Berhasil Di Ubah";
				
					redirect('/driver/view/'. $drivid, 'refresh');
				
			}
		}
		/*if($this->input->post("btnSave")){
			$hasil = $this->_editDriver($drivid);
			var_dump($drivid);
			if($hasil) {
				$data['success'] = "Data Berhasil Di Ubah";
					redirect('/driver/edit/' .$drivid .'/s'.$sFlag , 'refresh');
			}
		}*/
		if($sFlag!=""){
			$data['success'] = "Data Berhasil Di Ubah";
		}
		$this->load->view("driveredit",$data);	
	}
	
	function _editDriver($drivid){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'barcode',
                     'label'   => 'barcode',
                     'rules'   => 'trim|required|xss_clean'
                  )
				
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_driver->editBarang($drivid);
			return true;
		}
	}
	
	/*
			=================Function Delete Driver=================
	*/
	function delete(){
		$id = $this->uri->segment(3, 0);
		$position = $this->uri->segment(4, 0);
		$this->model_driver->delDataBarang($id);
		if($position === "fromadd"){
			redirect('/driver/index/d');
		}else
		{
			redirect('/driver/view/d');
		}
		
	}
	
	function lembur($set="sot"){
		$this->load->model("model_invoice");
		$data['active_invoice'] = 'y';
		$data['title'] = TITLE.' - Buat Form Lembur';
		$data['javascript'][] = "hitunglembur.js";
		$data['owner'] = $this->model_invoice->getOwnerList();
		$data['driver'] = $this->model_invoice->getDriverList();
		$data['max'] = $this->model_driver->checkLembur();
		$data['invoTambah'] = explode("LMBR", $data['max']);
		$data['customer'] = $this->model_invoice->getCustomerList();
		$data['perusahaan']=$this->model_invoice->getPtList();
		$data['staff']=$this->model_invoice->getDriverList();
		$data['payterm']=$this->model_invoice->getPayTerms();
		//$invoice = $data['max'];
		//$set = $this->uri->segment(4, 0);
		if($set=="set"){
			$data['success'] = "Form Berhasil di Simpan";
		}

		if($this->input->post("btnSave") ){
			
			$hasil = $this->_createInvoice();
			if($hasil){
				 $data['success'] = "Form Lembur Beharsil di Buat";
				 $data['max1'] = $this->model_invoice->checkInvoice();
				 
			};
			
		}
		else if($this->input->post("btnGener") ){
			$hasil = $this->_createFormLembur();
			if($hasil){
				 $data['success'] = "Form Lembur di Buat";
				 //$data['max1'] = $this->model_invoice->checkInvoice();
				 $this->load->view("lemburAdd",$data);
			};
			redirect("/PdfprinterLembur/pdfPrint/". $this->input->post("noInvoiceL", true). "/" . $this->input->post("salesPerson", true)
			. "/" . $this->input->post("mataUang", true) . "/" 
						. $this->input->post("customer", true) . "/" . $this->input->post("dateNow", true). "/" 
						 . "/" . $this->input->post("delivDate", true). "/" 
						. $this->input->post("shipto", true) . "/" . $this->input->post("billto", true). "/"
						. $this->input->post("delivTerm", true) . "/" . $this->input->post("payterm", true). "/"
						. $this->input->post("ptName", true) . "/" . $this->input->post("order", true). "/add/"
						. $this->input->post("salesPerson", true)
						);
			//$this->pdfPrint($data['max']);
			
			
		}
		$this->load->view("lemburadd",$data);
	
	}
	
	function _createFormLembur(){
		//$this->form_validation->set_message('_checkpassword', 'Current Password is incorrect');
		//$this->form_validation->set_message('_check_default', 'Harus dipilih salah satu');
		/*$this->form_validation->set_message('_checkDateKembali', 
											'Tanggal kembali lebih cepat dari tanggal pinjam');
		$this->form_validation->set_message('_checkDateSewa', 'Tidak ada mobil tersedia pada tanggal tersebut');*/
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'mataUang',
                     'label'   => 'Nama Perusahaan',
                     'rules'   => 'trim|xss_clean'
                  ),
			 	/*array(
                     'field'   => 'customer',
                     'label'   => 'Nama Peminjam',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				array(
                     'field'   => 'supir',
                     'label'   => 'Nama Supir',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'telepon_peminjam',
                     'label'   => 'Telepon Peminjam',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				array(
                     'field'   => 'tanggal_pinjam',
                     'label'   => 'Tanggal Pinjam',
                     'rules'   => 'trim|required|xss_clean|callback__checkDateSewa[]'
                  ),
				array(
                     'field'   => 'tanggal_kembali',
                     'label'   => 'Tanggal Kembali',
                     'rules'   => 'trim|required|xss_clean|callback__checkDateKembali['.$this->input->post('tanggal_pinjam').']'
                  ),
				array(
                     'field'   => 'ownerName',
                     'label'   => 'Nama Pemilik',
                     'rules'   => 'trim|required|callback__check_default|xss_clean'
                  ),
				 array(
                     'field'   => 'carList1',
                     'label'   => 'Mobil yang Dipakai',
                     'rules'   => 'trim|required|callback__check_default|xss_clean'
                  ),
				array(
                     'field'   => 'penerimaan',
                     'label'   => 'Penerimaan',
                     'rules'   => 'trim|required|integer|xss_clean'
                  )*/
            );

		$this->form_validation->set_rules($config); 
		
		if ($this->form_validation->run() == FALSE){
			
			return false;
			
		}
		else{
			//save to database..
			$this->model_driver->addLembur();
			return true;
		}
	}	
}